//
//  LimitedTimeDiscountCard.swift
//  CMB250716
//
//  Created by System on 2024/12/19.
//

import SwiftUI

/// 限时半价优惠促销卡片
struct LimitedTimeDiscountCard: View {
    @StateObject private var discountManager = LimitedTimeDiscountManager.shared
    @EnvironmentObject private var dataManager: DataManager
    @Binding var showingMenuView: Bool
    @State private var showingDetailSheet = false
    
    private let cardHeight: CGFloat = 120  // 与Cash Voucher卡片高度保持一致
    private let cornerRadius: CGFloat = 15  // 与Cash Voucher卡片圆角保持一致
    
    var body: some View {
        Button(action: {
            showingDetailSheet = true
        }) {
            ZStack {
                // 背景渐变
                backgroundGradient
                
                // 装饰性背景图案
                backgroundPattern
                
                // 主要内容
                mainContent
                
                // 半价标识（包含时间标识）
                discountBadge
            }
        }
        .buttonStyle(PlainButtonStyle())
        .frame(height: cardHeight)
        .frame(maxWidth: .infinity)  // 卡片宽度自适应手机宽度，左右不留空隙
        .padding(.horizontal, -10)  // 左右外边框扩展 10 个单位
        .clipShape(RoundedRectangle(cornerRadius: cornerRadius))
        .overlay(
            RoundedRectangle(cornerRadius: cornerRadius)
                .stroke(
                    LinearGradient(
                        colors: [.yellow.opacity(0.8), .orange.opacity(0.6)],
                        startPoint: .topLeading,
                        endPoint: .bottomTrailing
                    ),
                    lineWidth: 2
                )
        )
        .shadow(
            color: .black.opacity(0.3),
            radius: 15,
            x: 0,
            y: 8
        )
        .scaleEffect(discountManager.isDiscountActive ? 1.0 : 0.98)
        .opacity(discountManager.isDiscountActive ? 1.0 : 0.7)
        .animation(.easeInOut(duration: 0.3), value: discountManager.isDiscountActive)
        .sheet(isPresented: $showingDetailSheet) {
            LimitedTimeDiscountDetailSheet()
                .environmentObject(dataManager)
        }
    }
    
    // MARK: - Background Components
    
    private var backgroundGradient: some View {
        LinearGradient(
            colors: [
                Color(red: 0.8, green: 0.2, blue: 0.2), // 深红
                Color(red: 0.6, green: 0.1, blue: 0.4), // 紫红
                Color(red: 0.9, green: 0.4, blue: 0.1), // 橙色
                Color(red: 0.8, green: 0.6, blue: 0.0)  // 金色
            ],
            startPoint: .topLeading,
            endPoint: .bottomTrailing
        )
    }
    
    private var backgroundPattern: some View {
        ZStack {
            // 星星装饰
            ForEach(0..<8, id: \.self) { index in
                Image(systemName: "star.fill")
                    .font(.system(size: CGFloat.random(in: 8...16)))
                    .foregroundColor(.white.opacity(0.1))
                    .position(
                        x: CGFloat.random(in: 20...350),
                        y: CGFloat.random(in: 20...180)
                    )
                    .rotationEffect(.degrees(Double.random(in: 0...360)))
            }
            
            // 圆形装饰
            ForEach(0..<5, id: \.self) { index in
                Circle()
                    .fill(.white.opacity(0.05))
                    .frame(width: CGFloat.random(in: 30...80))
                    .position(
                        x: CGFloat.random(in: 0...370),
                        y: CGFloat.random(in: 0...200)
                    )
            }
        }
    }
    
    // MARK: - Main Content
    
    private var mainContent: some View {
        HStack(spacing: 20) {
            // 左侧内容
            VStack(alignment: .leading, spacing: 8) {
                // 主标题
                Text(dataManager.currentLanguage == "zh" ? 
                     AppConfig.LimitedTimeDiscount.promotionTitle.chinese :
                     AppConfig.LimitedTimeDiscount.promotionTitle.english)
                    .font(.system(size: 24, weight: .bold, design: .rounded))
                    .foregroundColor(.white)
                    .shadow(color: .black.opacity(0.5), radius: 2, x: 1, y: 1)
                
                // 副标题
                Text(dataManager.currentLanguage == "zh" ? 
                     AppConfig.LimitedTimeDiscount.promotionSubtitle.chinese :
                     AppConfig.LimitedTimeDiscount.promotionSubtitle.english)
                    .font(.system(size: 14, weight: .medium))
                    .foregroundColor(.white.opacity(0.9))
                    .shadow(color: .black.opacity(0.3), radius: 1, x: 0, y: 1)
                
                // 描述文本
                Text(dataManager.currentLanguage == "zh" ? 
                     AppConfig.LimitedTimeDiscount.promotionDescription.chinese :
                     AppConfig.LimitedTimeDiscount.promotionDescription.english)
                    .font(.system(size: 12, weight: .regular))
                    .foregroundColor(.white.opacity(0.8))
                    .lineLimit(2)
                    .multilineTextAlignment(.leading)
                
                Spacer()
            }
            .padding(.vertical, 12)
            
            Spacer()
        }
        .padding(.leading, 20)  // 左侧内边距缩短 20 个单位
        .padding(.trailing, 20)  // 右侧内边距缩短 20 个单位
    }
    

    

    
    // MARK: - Overlay Components
    

    
    private var discountBadge: some View {
        VStack {
            HStack {
                Spacer()
                
                VStack(spacing: 6) {  // 调整间距使排版更紧凑
                    // 50% OFF 圆形标识 - 缩小到90%
                    VStack(spacing: 2) {
                        Text("50%")
                            .font(.system(size: 25, weight: .black, design: .rounded))  // 字体稍微缩小
                            .foregroundColor(.white)
                        Text(dataManager.currentLanguage == "zh" ? "折扣" : "OFF")
                            .font(.system(size: 11, weight: .bold))  // 字体稍微缩小
                            .foregroundColor(.white.opacity(0.9))
                    }
                    .padding(10)  // 内边距稍微缩小
                    .background(
                        Circle()
                            .fill(
                                RadialGradient(
                                    colors: [.yellow, .orange, .red],
                                    center: .center,
                                    startRadius: 4,  // 渐变半径稍微缩小
                                    endRadius: 36    // 渐变半径稍微缩小
                                )
                            )
                            .shadow(color: .black.opacity(0.4), radius: 5, x: 2, y: 2)  // 阴影稍微缩小
                    )
                    .overlay(
                         Circle()
                             .stroke(.white.opacity(0.3), lineWidth: 2)
                     )
                     .scaleEffect(0.9)  // 整体缩小到90%
                     
                     // 时钟图标 + Time Limited 标识（移动到50% Off下方）
                     HStack(spacing: 4) {
                         Image(systemName: "timer")
                             .font(.system(size: 10, weight: .bold))
                         Text(dataManager.currentLanguage == "zh" ? "限时" : "TIME LIMITED")
                             .font(.system(size: 9, weight: .bold))
                     }
                     .foregroundColor(.white)
                     .padding(.horizontal, 6)
                     .padding(.vertical, 3)
                     .background(
                         Capsule()
                             .fill(.red.opacity(0.8))
                             .shadow(color: .black.opacity(0.3), radius: 1, x: 0, y: 1)
                     )
                 }
            }
            .padding(.top, 10)  // 调整顶部间距
            .padding(.trailing, 20)  // 右侧内边距缩短 20 个单位
            
            Spacer()
        }
    }
}

// MARK: - Preview

struct LimitedTimeDiscountCard_Previews: PreviewProvider {
    static var previews: some View {
        VStack(spacing: 20) {
            LimitedTimeDiscountCard(showingMenuView: .constant(false))
                .environmentObject(DataManager.shared)
            
            LimitedTimeDiscountCard(showingMenuView: .constant(false))
                .environmentObject(DataManager.shared)
                .preferredColorScheme(.dark)
        }
        .padding()
        .background(Color.gray.opacity(0.1))
    }
}